var express = require('express');
var router = express.Router();

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = "mongodb://localhost:27017/douban"; 

var async = require('async');


router.get('/details',function(req,res,next) { //跳转详情页
  // res.send('id'+req.query.id);

  
  MongoClient.connect(DB_CONN_STR,function(err,db){
    if(err){
      return;
    }else{
      findData(db,function(err,result){
        for(var i=0;i<result.length;i++){
          if(result[i].length==0){
            continue;
          }else{
            var data=result[i][0];
          }
        }
        console.log(data);
        res.render('details',{det:data});
        db.close();
      })
    };
  });

  function findData(db,cb){
    async.parallel([
      function(cb){
        var conn = db.collection('in_theaters');
        var data = {id:req.query.id};
        conn.find(data).toArray(function(err,result){
          if(err){
            return;
          }else{
            cb(null,result);
          }
        })
      },function(cb){
        var conn = db.collection('coming_soon');
        var data = {id:req.query.id};
        conn.find(data).toArray(function(err,result){
          if(err){
            return;
          }else{
            cb(null,result);
          }
        })
      },function(cb){
        var conn = db.collection('top250');
        var data = {id:req.query.id};
        conn.find(data).toArray(function(err,result){
          if(err){
            return;
          }else{
            cb(null,result);
          }
        })
      },function(cb){
        var conn = db.collection('us_box');
        var data = {id:req.query.id};
        conn.find({subject:data}).toArray(function(err,result){
          if(err){
            console.log(err);
            return;
            
          }else{
            cb(null,result);
          }
        })
      }
    ],function(err,result){
      cb(err,result);
    })
  }
})

module.exports=router;